"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具设置dss节点的auid
Create At   : 2023/4/21
Owner       : opentestcase019
Description :
    1.设置dss节点的auid，查看帮助信息
    2.设置dss节点的auid，不指定参数
    3.设置dss节点的auid，指定-a auid参数为有效值
    4.设置dss节点的auid，指定-a auid参数为无效值
    5.在dss关闭的情况下执行设置dss节点的auid
    6.在集群停止的情况下执行
    7.DSS_HOME路径权限不足时，设置dss节点的auid
    8.在备机设置dss节点的auid
Expect      :
    1.查看成功
    2.合理报错
    3.设置成功
    4.合理报错
    5.设置成功
    6.设置成功
    7.设置成功
    8.设置成功
History     :
"""
import os
import time
import re
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()
        self.expect = "id:[0-9]*:\n  volumeid:[0-9]*\n  auid:[0-9]*\n  " \
                      "blockid:0\n  item:0"

    def test_sharestorage_tool(self):
        step1 = "-----step1:设置dss节点的auid，查看帮助信息;expect:查看成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd auid -h"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Usage:dsscmd auid <-a auid>\n[tool command] show auid\n'
                      '-a/--auid <auid>, <required>, the auid will to show',
                      execute_msg, "执行失败" + step1)

        step2 = "-----step2:设置dss节点的auid，不指定参数;expect:合理报错-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd auid;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('args [-a|--auid] needs input value.',
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3:设置dss节点的auid，指定-a auid参数为有效值;expect:设置成功-----"
        self.logger.info(step3)
        cmd_list = ['-a 0', '-a 1242', '-a "43"',
                    '--auid 0', '--auid "124"', '--auid 200']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd auid {cmd};"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(
                self.primary_node, execute_cmd)
            search_res = re.search(self.expect, execute_msg, re.S)
            self.assertIsNotNone(search_res, '执行失败' + step3)

        step4 = "-----step4:设置dss节点的auid，指定-a auid参数为无效值;expect:合理报错-----"
        self.logger.info(step4)
        cmd_list = ['-1', '中', 'zero', "'%&@'", '1@']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd auid -a {cmd};" \
                          f"dsscmd auid --auid {cmd}"
            self.logger.info(execute_cmd)
            res = self.common.get_sh_result(self.primary_node, execute_cmd)
            self.assertIn(
                'id_str:{} is not a valid uint64'.format(cmd.strip("'")),
                res, "执行失败" + step4)
            self.assertIn('Value error: Convert uint64 failed, the text is '
                          'not number', res, '执行失败' + step4)

        step5 = "-----step5:在dss关闭的情况下执行设置dss节点的auid;expect:设置成功-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd stopdss;" \
                      f"dsscmd auid -a 237;" \
                      f"dsscmd auid --auid 300;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg,
                      "执行失败" + step5)
        search_res = re.search(self.expect, execute_msg, re.S)
        self.assertIsNotNone(search_res, '执行失败' + step5)

        step6 = "-----step6:在集群停止的情况下执行;expect:设置成功-----"
        self.logger.info(step6)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd auid -a 237;" \
                      f"dsscmd auid --auid 300;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        search_res = re.search(self.expect, execute_msg, re.S)
        self.assertIsNotNone(search_res, '执行失败' + step6)
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败')

        step7 = "-----step7:DSS_HOME路径权限不足时，设置dss节点的auid;expect:成功-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"chmod 000 {macro.DSS_INSTANCE};" \
                      f"dsscmd auid -a 200;" \
                      f"dsscmd auid --auid 166;" \
                      f"chmod 700 {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        search_res = re.search(self.expect, execute_msg, re.S)
        self.assertIsNotNone(search_res, '执行失败' + step7)

        step8 = "-----step8:在备机设置dss节点的auid;expect:设置成功-----"
        self.logger.info(step8)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd auid -a 19;" \
                      f"dsscmd auid --auid 314;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        search_res = re.search(self.expect, execute_msg, re.S)
        self.assertIsNotNone(search_res, '执行失败' + step8)

    def tearDown(self):
        self.logger.info("-----无需清理环境-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
